<?php
defined('BASEPATH') OR exit('No direct script access allowed');

//课程卡
class Card extends MY_Controller {
		
	function __construct(){
		parent::__construct();
		
		$this->load->model('Index/CourseCardModel', 'item');
	}	
	
	//start
	/**
	* @name 购卡列表
	* @param page true int page
	* @param size true int size
	* @param cid true int cid
	* @return 
	*/
	//end
	public function get_list(){		
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');
		$cid		= $this->input->post('cid');
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"cid" => $cid
		);
		
		$res = $this->item->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
	//详细
	public function get_info(){
		$id		= $this->input->post('id',null,'0');
		$info = $this->item->get_info($id);
		if(!$info){
			$this->err_out('数据不存在');
		}
		
		$res = $this->item->get_info($id);
		
		$data['result_code'] = 0;
		$data['message']     = '成功';
		$data['data'] 		 = $res;
	
		$this->out($data);
	}
	
	//全部
	public function get_lists(){
		$cid = $this->input->post('cid');
		$arr = array(
			"cid" => $cid
		);
		$res = $this->item->get_lists($arr);
		
		$data['result_code'] = 0;
		$data['message']     = '获取成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	

	//start
	/**
	* @name 购卡
	* @param id true int 卡ID
	* @param form_id true varchar form_id
	* @return 
	*/
	//end
	public function buy_card(){
		
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,0);//卡id
		$form_id	= $this->input->post('form_id',null,'');//卡id
		
		
		$this->load->model('Index/PayLogModel', 'paylog');
		$this->load->model('Admin/ConfigModel', 'cfg');
		$this->load->model('Index/CourseCardModel', 'card');
		$card_info = $this->card->get_info($id);
		if(!$card_info){
			$this->err_out('数据不存在');
		}
		$this->load->library('Weixin');
		$card_info['form_id'] = $form_id;
		$pay_info = $this->unifiedorder($card_info);
		
		
		$time = time();
		$nonceStr  = $this->weixin->make_password(32);
		$arr = array(
			'appId'=>$pay_info['appId'],
			'nonceStr'=>$nonceStr,
			'package'=>'prepay_id='.$pay_info['prepay_id'], //统一下单返回的订单号
			'signType'=>'MD5',
			'timeStamp'=>$time
		);
		
		$mch_key = $this->cfg->get_config('mch_key');
		if(!$mch_key){
			$this->err_out('请先配置商户KEY');
		}
		
		$signValue = $this->weixin->get_sign($arr,$mch_key);
		$arr['paySign'] = $signValue;
		
		$this->out(array('data'=>$arr));
	}
	
	//统一下单
	function unifiedorder($card_info){
		
		$user_id = $this->user_id;
		$money = $card_info['price'];
		
		$pay_info = $this->paylog->add_log($user_id,$money,$card_info);
		if(!$pay_info){
			$this->err_out('插入支付错误');
			return false;
		}
		

		$appid = $this->cfg->get_config('UserAppID');
		if(!$appid){
			$this->err_out('请先配置AppID');
		}
		$mch_id = $this->cfg->get_config('mch_id');
		if(!$mch_id){
			$this->err_out('请先配置商户ID');
		}
		$notify_url = $this->cfg->get_config('notify_url');
		if(!$notify_url){
			$this->err_out('请先配置支付回调');
		}
		$mch_key = $this->cfg->get_config('mch_key');
		if(!$mch_key){
			$this->err_out('请先配置商户KEY');
		}
		
		if(substr($notify_url,0,4)!='http'){
			$notify_url = base_url($notify_url);
		}
		
		//$aa = file_get_contents($notify_url);
		//var_dump($aa);
		//exit;
		
		$this->load->model('User/UserModel', 'user');
		$user = $this->user->get_info($user_id);
		if(!$user){
			$this->err_out('用户不存在');
		}

		$openid = $user['openid'];

		$nonceStr  = $this->weixin->make_password(32);
		$arr = array(
			'appid'=>$appid,//小程序ID
			'mch_id'=>$mch_id,//商户号
			'nonce_str'=>$nonceStr,//随机字符串
			'sign_type'=>'MD5',//签名类型
			'body'=>$card_info['card_name'],//商品描述
			'out_trade_no'=>$pay_info['order_sn'],//商户订单号
			'total_fee'=>$pay_info['amount'] * 100, //订单总金额，单位为分，详见支付金额
			'spbill_create_ip'=>'123.12.12.123', //APP和网页支付提交用户端ip，Native支付填调用微信支付API的机器IP
			'notify_url'=>$notify_url,//通知地址
			'trade_type'=>'JSAPI',
			'openid'=>$openid,
			//'detail'=>''
		);
		$signValue = $this->weixin->get_sign($arr,$mch_key);
		$arr['sign'] = $signValue;//签名
		
		
		$xml = $this->weixin->data_to_xml($arr);
		$data = array('data'=>$xml);
		//var_dump($data);exit;
		$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
		$result = $this->weixin->post_curls($url,$data);
		
		$result_data = $this->weixin->xmlToArray($result);
		if($result_data['return_code']!='SUCCESS'){
			$this->err_out($result_data['return_msg']);
		}
		
		if($result_data['result_code']!='SUCCESS'){
			$this->err_out('接口提交成功，但是业务结果错误');
		}
		
		$arr = array(
			'appId'=>$appid,
			'trade_type'=>$result_data['trade_type'],
			'prepay_id'=>$result_data['prepay_id'],
			
		);
		return $arr;
	}
	//购卡(测试)
	public function buy_card_bak(){
		$user_id = $this->user_id;
		$id	= $this->input->post('id',null,'0');//卡id
		$time = time();		
		
		$this->load->model('Index/CourseCardModel', 'card');
		$card_info = $this->card->get_info($id);
		if(!$card_info){
			$this->err_out('数据不存在');
		}
		
		
		$this->load->model('User/UserCardModel', 'ucard');		
		$user_card = array(
			"user_id"  => $user_id,
			"cid"	   => $card_info['course_id'],
			"card_id"  => $id,
			"total_num"=> $card_info['course_num'],
			"num"	   => $card_info['course_num'],
			"price"    => $card_info['price'],			
			"addtime"  => $time
		);		
		$res = $this->ucard->add($user_card);
		
		if($res){
			$this->load->model('User/UserCardLogModel', 'card_log');
			$card_log = array(
				"user_id"  => $user_id,				
				"card_id"  => $id,				
				"price"    => $card_info['price'],
				"num"      => $card_info['course_num'],
				"info"	   => '购卡成功！',
				"addtime"  => $time
			);
			$res_log = $this->card_log->add($card_log);
		}
		
		$data['result_code'] = 0;
		$data['message']     = '购买成功';
		$data['data'] 		 = $res;	
		$this->out($data);
	}
	
	//start
	/**
	* @name /购卡记录
	* @param page true int page
	* @param size true int size
	* @return 
	*/
	//end
	public function buy_card_log(){
		$user_id = $this->user_id;
		
		$page		= $this->input->post('page');
		$size		= $this->input->post('size');		
		
		if($page<1)$page=1;
		if($size<1)$size=10;
		
		$arr = array(
			"user_id" => $user_id
		);
		
		$this->load->model('User/UserCardLogModel', 'card_log');
		$res = $this->card_log->get_list($arr,$page,$size);
		
		if(($page-1)*$size>$res['count']){
			$this->err_out('没有了',10);
		}
	
		$data['result_code'] = 0;
		$data['message']     = '获取列表成功';
		$data['data'] = array('count'=>$res['count'],'list'=>$res['list']);
	
		$this->out($data);
	}
	
}
